package io.noties.markwon.base.core.spans

import android.text.TextPaint
import android.text.style.MetricAffectingSpan
import com.gitee.wsl.ext.base.setAlpha
import io.noties.markwon.theme.MarkwonTheme

/**
 * @since 3.0.0 split inline and block spans
 */
class CodeSpan(private val theme: MarkwonTheme) : MetricAffectingSpan() {
    override fun updateMeasureState(p: TextPaint) {
        apply(p)
    }

    override fun updateDrawState(ds: TextPaint) {
        apply(ds)
        var color = theme.colorTheme.codeBackgroundColor.toInt()
        if (color == 0) {
            color = ds.color.setAlpha(CODE_DEF_BACKGROUND_COLOR_ALPHA)
        }
        ds.bgColor = color
    }

    private fun apply(p: TextPaint) {
        applyCodeTextStyle(p)
    }

    private fun applyCodeTextStyle(p: TextPaint) {}

    companion object {
        protected const val CODE_DEF_BACKGROUND_COLOR_ALPHA = 25
    }
}